package com.yao.longhan.presenter.holder;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.yao.longhan.BaseApplication;
import com.yao.longhan.R;
import com.yao.longhan.constant.Constant;
import com.yao.longhan.dao.StoryArticleDao;
import com.yao.longhan.eventbus.AddToShelfEvent;
import com.yao.longhan.eventbus.RxBus;
import com.yao.longhan.model.StoryArticle;
import com.yao.longhan.presenter.BaseHolder;
import com.yao.longhan.utils.SharedPreferencesUtil;
import com.yao.longhan.utils.ToastUtils;

import butterknife.BindView;

/**
 * Created by heyao on 2017/7/31.
 */

public class HomeHolder extends BaseHolder<StoryArticle> {
    private final StoryArticleDao mDao;
    @BindView(R.id.item_home_image)
    ImageView item_home_image;
    @BindView(R.id.item_home_text)
    TextView item_home_text;

    public HomeHolder(View itemView, BaseApplication application) {
        super(itemView, application);
        mDao = BaseApplication.getInstance().getDaoSession().getStoryArticleDao();
    }

    @Override
    public void bindData(int position, StoryArticle book) {
        item_home_text.setText(book.getArtName());
        itemView.setOnClickListener(view -> {
            int readCount = SharedPreferencesUtil.getInstance().getInt(Constant.BOOK_SHELF_SORT_KEY, 0) + 1;
            SharedPreferencesUtil.getInstance().putInt(Constant.BOOK_SHELF_SORT_KEY, readCount);
            book.setReadCount(readCount);
            mDao.update(book);
            RxBus.getDefault().post(new AddToShelfEvent());
            ToastUtils.showToast("Start ReadActivity1.");
        });
    }
}
